#include "stdafx.h"
#include "InitVariable.H"
#include <Windows.h>
#include <creg.hxx>
#include "GUIFile.h"

TCHAR *VRMFolder[]=
{
	L"Movie1",
	L"Movie2",
	L"Movie3",
	L"Movie4",
	L"Cam1",
	L"Broad1",	
	L"Broad2",	
	NULL
};

bool CheckImageRotatorDriver()				// for TCMovieManager
{
	bool rt = false;
	CReg regFolder;

	if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\IRD") )
	{
		rt = true;
	}
	
	return rt;
}

bool CheckMultiInstance()
{
	CReg regFolder;
	if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\MSC") )
	{
		if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\VCU") )
		{
			return true;
		}
	}
	return false;
}

bool GetMultiDisplay()				// Get
{
	bool rt = false;
	if (!CheckMultiInstance())
	{
		return false;
	}
	CReg regFolder;
	if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\MSC") )
	{
		rt = regFolder.ValueDW(L"MultiDisplay");		
	}
	else
	{
		return false;		// Get Fail
	}
	return rt;
}

bool SetMultiDisplay(bool bEnable)				// Set
{
	bool rt = false;
	if (!CheckMultiInstance())
	{
		return false;
	}
	CReg regFolder;
	if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\MSC") )
	{
		regFolder.SetDW(L"MultiDisplay", bEnable);		
	}
	else
	{
		return false;		// Set Fail
	}
	return rt;
}


bool GetVRMState()
{
	bool rt = false;
	CReg regFolder;
	if (CheckMultiInstance())
	{
		if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\MSC"))
		{
			rt = regFolder.ValueDW(L"useVRM");			
		}
	}
	return rt;
}

bool SetVRMState(int nEnableVRM)
{
	bool rt = false;
	CReg regFolder;
	if (CheckMultiInstance())
	{
		if (regFolder.Open(HKEY_LOCAL_MACHINE, L"Drivers\\BuiltIn\\MSC"))
		{
			regFolder.SetDW(L"useVRM", (DWORD)nEnableVRM);			
		}
	}
	return rt;
}

bool LoadVRM(stVRM *pstVRM)
{
	bool rt = false;
	CReg regFolder;
	TCHAR KeyString[128];
	int i = 0;
	if (CheckMultiInstance())
	{
		for (i = 0; VRMFolder[i] != NULL; i++)
		{
			wsprintf((LPWSTR)KeyString, L"Drivers\\BuiltIn\\MSC\\%s", VRMFolder[i]);

			if (regFolder.Open(HKEY_LOCAL_MACHINE, KeyString) )
			{
				pstVRM[i].left	= regFolder.ValueDW(L"left");
				pstVRM[i].right	= regFolder.ValueDW(L"right");
				pstVRM[i].top	= regFolder.ValueDW(L"top");
				pstVRM[i].bottom= regFolder.ValueDW(L"bottom");
				pstVRM[i].LCDC	= regFolder.ValueDW(L"LCDC");
				pstVRM[i].Image	= regFolder.ValueDW(L"Image");
				pstVRM[i].Use	= regFolder.ValueDW(L"Use");
			}
			else
			{
				return false;
			}
		}
	}
	return rt;
}

bool SaveVRM(stVRM *pstVRM)
{
	bool rt = false;
	CReg regFolder;
	TCHAR KeyString[128];
	int i = 0;
	if (CheckMultiInstance())
	{
		for (i = 0; VRMFolder[i] != NULL; i++)
		{
			wsprintf((LPWSTR)KeyString, L"Drivers\\BuiltIn\\MSC\\%s", VRMFolder[i]);
			regFolder.Create(HKEY_LOCAL_MACHINE, KeyString);
			if (regFolder.Open(HKEY_LOCAL_MACHINE, KeyString))
			{
				regFolder.SetDW(L"left",	(DWORD)pstVRM[i].left);
				regFolder.SetDW(L"right",	(DWORD)pstVRM[i].right);
				regFolder.SetDW(L"top",		(DWORD)pstVRM[i].top);
				regFolder.SetDW(L"bottom",	(DWORD)pstVRM[i].bottom);
				regFolder.SetDW(L"LCDC",	(DWORD)pstVRM[i].LCDC);
				regFolder.SetDW(L"Image",	(DWORD)pstVRM[i].Image);
				regFolder.SetDW(L"Use",		(DWORD)pstVRM[i].Use);
			}			
		}
		rt = true;
	}
	return rt;
}

void LoadLCDSize(int *Width,int *Height)
{
	HKEY    hk;
	DWORD   dwStatus;
	DWORD dwSize,dwType;

	dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Drivers\\Display\\TCC78X\\CONFIG", 0, 0, &hk);
	dwType = REG_DWORD;

	if( ERROR_SUCCESS != dwStatus ) {
		dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Drivers\\Display\\TCC79X\\CONFIG", 0, 0, &hk);
		if( ERROR_SUCCESS != dwStatus )
		{
			dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Drivers\\Display\\Magellan\\CONFIG", 0, 0, &hk);
			if( ERROR_SUCCESS != dwStatus )
			{
				dwStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Drivers\\Display\\Astrid\\CONFIG", 0, 0, &hk);
				if( ERROR_SUCCESS != dwStatus )
				{
					MessageBox(NULL, _T("Couldn't get LCD size from registry"),0,0);
					return;
				}
			}
		}
	}

	if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) 
	{
		dwSize = sizeof(DWORD);
		dwStatus = RegQueryValueEx(hk, _T("LCD_Width"), NULL, &dwType, (LPBYTE) Width, &dwSize);
	}

	if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) 
	{
		dwSize = sizeof(DWORD);
		dwStatus = RegQueryValueEx(hk, _T("LCD_Height"), NULL, &dwType, (LPBYTE) Height, &dwSize);
	}

	if(hk != NULL) 
	{
		RegCloseKey(hk);
	}
}


int CreateConfigFolder(TCHAR *pszStorageName)
{
	WIN32_FIND_DATA FindFileData; 
	HANDLE hFind;
	TCHAR szFullPath[MAX_PATH];

	wsprintf(szFullPath,TEXT("\\%ls\\Config"),pszStorageName);

	hFind = FindFirstFile(szFullPath,&FindFileData);
	if(hFind == INVALID_HANDLE_VALUE)
	{
		if(!::CreateDirectory(szFullPath,NULL))
		{
//			AfxMessageBox(TEXT("[TCMovieManager]Fail to create Config folder"),MB_OK,NULL);
		}

		return 0;
	}

	FindClose(hFind);

	return 1; 
}


int LoadInitVariable(TCHAR *pszStorageName,
					 unsigned int *PlayStatus
					  ,unsigned int *CurentPlayFileIndex
					  ,unsigned int *CurVolume
					  ,double *CurPlayTime
					  ,unsigned int *RepeatMode
					  ,BOOL *bNoRepeat
					  ,unsigned int *DisplayMode
					  ,unsigned int *AutoStart
					  ,double *PlayDuration
					  ,int *pScreenSmallMode
					  ,int *TVOutMode
					  ,int *SupportFiles
					  ,BOOL *bMessageBox
					  ,int *bRandomPlay
					  ,int *bPrintInfo
					  ,int *nSelectedUI
					  ,int *nIntroModeSec
					  ,BOOL *bChromakeyEnable
					  ,int *nRvalue
					  ,int *nGvalue
					  ,int *nBvalue
					  ,TCHAR *szCurrentVolume
					  ,TCHAR *szLastFolder)
{
	CGUIFile InitFile;

	if(!::CreateConfigFolder(pszStorageName))
	{
		*CurVolume = 7;
		*bMessageBox = TRUE;
		*bRandomPlay = false;
		*bPrintInfo = false;
		*bNoRepeat = FALSE;		// repeat 
		*nIntroModeSec = 0;
		return 0;
	}

	TCHAR szFullPath[MAX_PATH];

	wsprintf(szFullPath,TEXT("\\%ls\\Config\\Config_TCMovieManager_%d_%d.dat"),pszStorageName, TWS, VER);

	if(InitFile.Open(szFullPath))
	{
		InitFile.Read(RepeatMode,sizeof(unsigned int));
		InitFile.Read(PlayStatus,sizeof(unsigned int));
		InitFile.Read(CurentPlayFileIndex,sizeof(unsigned int));
		InitFile.Read(CurVolume,sizeof(unsigned int));
		InitFile.Read(CurPlayTime,sizeof(double));
		InitFile.Read(DisplayMode,sizeof(int));
		InitFile.Read(AutoStart,sizeof(int));
		InitFile.Read(PlayDuration,sizeof(double));
		int ScreenSmallMode;
		InitFile.Read(&ScreenSmallMode,sizeof(int));
		if(ScreenSmallMode == 1)
		{
			*pScreenSmallMode = 1;
		}

		InitFile.Read(TVOutMode,sizeof(int));
		InitFile.Read(SupportFiles,sizeof(int));
		InitFile.Read(bMessageBox,sizeof(BOOL));
		InitFile.Read(bRandomPlay,sizeof(int));
		InitFile.Read(bPrintInfo,sizeof(int));
		InitFile.Read(bNoRepeat,sizeof(BOOL));
		InitFile.Read(nSelectedUI, sizeof(int));
		InitFile.Read(nIntroModeSec, sizeof(int));
		InitFile.Read(bChromakeyEnable, sizeof(BOOL));
		InitFile.Read(nRvalue, sizeof(int));
		InitFile.Read(nGvalue, sizeof(int));
		InitFile.Read(nBvalue, sizeof(int));

		InitFile.Read(szCurrentVolume,	sizeof(TCHAR)*MAX_PATH);
		InitFile.Read(szLastFolder,		sizeof(TCHAR)*MAX_PATH);

		if(!AutoStart)
		{
			*PlayStatus = 0;
			*CurPlayTime = 0;
			*PlayDuration = 0;
			*nIntroModeSec = 0;
		}

		InitFile.Close();

		return 1;
	}
	else
	{
		*bPrintInfo = 0;
		*CurVolume = 7;
		*bMessageBox = TRUE;
		*bRandomPlay = false;
		*bNoRepeat = FALSE;		// repeat
		*nIntroModeSec = 0;
		*bChromakeyEnable = FALSE;
		*nRvalue = 16;
		*nGvalue = 0;
		*nBvalue = 16;		
	}

	return 0;
}

void SaveInitVariable(TCHAR *pszStorageName,
					  unsigned int PlayStatus
					  ,unsigned int CurentPlayFileIndex
					  ,unsigned int CurVolume
					  ,double CurPlayTime
					  ,unsigned int RepeatMode
					  ,BOOL bNoRepeat
					  ,unsigned int DisplayMode
					  ,unsigned int AutoStart
					  ,double PlayDuration
					  ,int ScreenSmallMode
					  ,int TVOutMode
					  ,int supportFiles
					  ,BOOL bMessageBox
					  ,int bRandomPlay
					  ,int bPrintInfo
					  ,int nSelectedUI
					  ,int nIntroModeSec
					  ,BOOL bChromakeyEnable
					  ,int nRvalue
					  ,int nGvalue
					  ,int nBvalue
					  ,TCHAR *szCurrentVolume
					  ,TCHAR *szLastFolder)
{
	CGUIFile InitFile;

	::CreateConfigFolder(pszStorageName);

	TCHAR szFullPath[MAX_PATH];

	wsprintf(szFullPath,TEXT("\\%ls\\Config\\Config_TCMovieManager_%d_%d.dat"),pszStorageName, TWS, VER);

	if(InitFile.Open(szFullPath))
	{
		if(!AutoStart)
		{
			PlayStatus = 0;
			CurPlayTime = 0;
			PlayDuration = 0;
		}

		InitFile.Write(&RepeatMode,sizeof(unsigned int));
		InitFile.Write(&PlayStatus,sizeof(unsigned int));
		InitFile.Write(&CurentPlayFileIndex,sizeof(unsigned int));
		InitFile.Write(&CurVolume,sizeof(unsigned int));
		InitFile.Write(&CurPlayTime,sizeof(double));
		InitFile.Write(&DisplayMode,sizeof(int));
		InitFile.Write(&AutoStart,sizeof(int));
		InitFile.Write(&PlayDuration,sizeof(double));
		InitFile.Write(&ScreenSmallMode,sizeof(int));
		InitFile.Write(&TVOutMode,sizeof(int));
		InitFile.Write(&supportFiles,sizeof(int));
		InitFile.Write(&bMessageBox,sizeof(BOOL));
		InitFile.Write(&bRandomPlay,sizeof(int));
		InitFile.Write(&bPrintInfo,sizeof(int));
		InitFile.Write(&bNoRepeat,sizeof(BOOL));
		InitFile.Write(&nSelectedUI, sizeof(int));
		InitFile.Write(&nIntroModeSec, sizeof(int));
		InitFile.Write(&bChromakeyEnable, sizeof(BOOL));
		InitFile.Write(&nRvalue, sizeof(int));
		InitFile.Write(&nGvalue, sizeof(int));
		InitFile.Write(&nBvalue, sizeof(int));

		InitFile.Write(szCurrentVolume,	sizeof(TCHAR)*MAX_PATH);
		InitFile.Write(szLastFolder,	sizeof(TCHAR)*MAX_PATH);

		InitFile.Close();
	}
	else
	{
		printf("[TCMovieManager]Fail : Write Init data\n");
	}

	return;
}